<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>地理定位</title>
    <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css" />
    <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=b924bdcb52ba818af5dc34c21ba657fa&plugin=AMap.Geocoder"></script>
</head>

<body>
    <div id="container"></div>
    <div id="tip">
        <b>经纬度 116.396574, 32.992706 的地理编码结果:</b>
        <span id="result"></span>
    </div>
    <script type="text/javascript">
    //var map, geolocation;
    //加载地图，调用浏览器定位服务
    window.onload = function() {
        eflower_location.Gdlocation(function(data) {
            var lnglatXY = new Array();
            lnglatXY[0] = data.position.getLng();
            lnglatXY[1] = data.position.getLat();
           // alert(lnglatXY);
            eflower_address.regeocoder(lnglatXY, function(data) {
                //alert(data)
            });
        });


    }

    // function regeocoder(lnglatXY) { 
    //     var geocoder = new AMap.Geocoder({
    //         radius: 1000,
    //         extensions: "all"
    //     });        
    //     geocoder.getAddress(lnglatXY, function(status, result) {
    //         if (status === 'complete' && result.info === 'OK') {
    //             geocoder_CallBack(result);
    //         }
    //     });        
    // }
    // function geocoder_CallBack(data) {
    //     var town = data.regeocode.addressComponent.township; //乡级
    //     var street = data.regeocode.addressComponent.street; //乡级
    //     var Address = data.regeocode.formattedAddress; //具体地址


    //     if(street!=''){
    //       console.log(street);
    //       return street;
    //     }else{
    //           console.log(town);
    //      return town;
    //     }
    // }

    eflower_address = {
            success: function(add) {
                return add
            },
            regeocoder: function(lnglatXY, _success) {
                var geocoder = new AMap.Geocoder({
                    radius: 1000,
                    extensions: "all"
                });
                geocoder.getAddress(lnglatXY, function(status, result) {
                    if (status === 'complete' && result.info === 'OK') {
                        eflower_address.geocoder_CallBack(result);
                    }
                });
                if (_success != undefined && typeof(_success) == "function") {
                    eflower_address.success = _success;
                };
            },
            geocoder_CallBack: function(data) {
                var town = data.regeocode.addressComponent.township; //乡级
                var street = data.regeocode.addressComponent.street; //乡级
                var Address = data.regeocode.formattedAddress; //具体地址
                var zhoubian=data.regeocode.aois[0].name;   //周边地址
                // console.log(data.regeocode.addressComponent)
                if(data.regeocode.addressComponent.city==''){
                      alert(data.regeocode.addressComponent.province)
                }else{
                     alert(data.regeocode.addressComponent.city)
                }
                // if (street != '') {
                //     eflower_address.success(street);
                // } else {
                //     eflower_address.success(town);
                // }
                 eflower_address.success(town);

            }

        }
        //第一个坐标点数组
        //第二个是回调函数

    // eflower_address.regeocoder(lnglatXY, function(data) {
    //     alert(data)
    // });


    //地址
        // eflower_address = {
        //     success: function(a) {
        //         return a
        //     },
        //     regeocoder: function(a, b) {
        //         (new AMap.Geocoder({
        //             radius: 1E3,
        //             extensions: "all"
        //         })).getAddress(a, function(a, b) {
        //             "complete" === a && "OK" === b.info && eflower_address.geocoder_CallBack(b)
        //         });
        //         void 0 != b && "function" == typeof b && (eflower_address.success = b)
        //     },
        //     geocoder_CallBack: function(a) {
        //         var b = a.regeocode.addressComponent.township;
        //         a = a.regeocode.addressComponent.street;
        //         "" != a ? eflower_address.success(a) : eflower_address.success(b)
        //     }
        // };






    eflower_location = {
        success: function(a) {},
        error: null,
        GdMapload: function() {
            var a = document.body,
                b = document.createElement("div");
            b.style.display = "none";
            b.setAttribute("id", "ContentMap");
            a.appendChild(b)
        },
        Gdlocation: function(a) {
            void 0 != a && "function" == typeof a && (eflower_location.success = a);
            eflower_location.GdMapload();
            map = new AMap.Map("container");
            map.plugin("AMap.Geolocation", function() {
                geolocation = new AMap.Geolocation({
                    enableHighAccuracy: !0,
                    timeout: 1E4
                });
                map.addControl(geolocation);
                geolocation.getCurrentPosition();
                AMap.event.addListener(geolocation, "complete", eflower_location.onComplete);
                AMap.event.addListener(geolocation, "error", eflower_location.onError)
            })
        },
        onComplete: function(a) {
            eflower_location.success(a)
        },
        onError: function(a) {
            null != eflower_location.error && "function" == typeof eflower_location.error ? eflower_location.error(a) : console.error("error")
        }
    };
//http://ditu.amap.com/service/poiTipslite?&city=310000&geoobj=121.445178%7C31.163867%7C121.609973%7C31.305363&words=%E7%A7%8B%E6%9C%88feng%27she
    </script>
</body>

</html>

